package com.wgj.android.ui.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiaai.xui.XUI;
import com.jiaai.xui.utils.XEmptyUtils;
import com.jiaai.xui.utils.XResourcesUtils;
import com.jiaai.xui.utils.http.HttpCallBack;
import com.jiaai.xui.utils.http.XHttp;
import com.jiaai.xui.utils.imageload.XImage;
import com.jiaai.xui.view.widget.XToast;
import com.wgj.android.R;
import com.wgj.android.entity.BankInfo;
import com.wgj.android.http.WGJAPI;
import com.wgj.android.utils.AssetsBankInfo;
import com.wgj.android.utils.CacheUtils;
import com.wgj.android.utils.GetBank;
import com.wgj.android.widget.AddSpaceTextWatcher;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Response;

public class BindActivity extends AppCompatActivity {
    @BindView(R.id.ed_num)
    EditText ed_num;
    @BindView(R.id.name_bank)
    TextView name_bank;
    @BindView(R.id.ed_name)
    EditText ed_name;
    @BindView(R.id.bankIcon)
    ImageView bankIcon;
    private  BankInfo bankInfo=new BankInfo();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        AddSpaceTextWatcher addSpaceTextWatcher=new AddSpaceTextWatcher(ed_num,48);
        addSpaceTextWatcher.setSpaceType(AddSpaceTextWatcher.SpaceType.bankCardNumberType);
        ed_num.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,int count) { }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                String huoqucc = ed_num.getText().toString().replace(" ","");
                if(huoqucc.length()==6){
                    String bankName= GetBank.getname(huoqucc);
                    if(bankName==null){
                        XToast.info("不支持的银行卡号");
                        return;
                    }
                    bankName=bankName.substring(0,bankName.indexOf("银行",0)+2);
                    String code=AssetsBankInfo.getID(bankName);
                    if(!TextUtils.isEmpty(code)){
                        bankInfo.setBank_code(code);
                        bankInfo.setBank_name(bankName);
                        name_bank.setText(bankName);
                        bankIcon.setImageDrawable(XUI.getDrawable(XResourcesUtils.getDrableId("icon_bank_"+code)));
                    }else {
                        XToast.info("不支持的银行卡号");
                        bankInfo.setBank_name("");
                        bankInfo.setBank_code("");
                    }
                }else if (huoqucc.length()<6){
                    bankInfo.setBank_name("");
                    bankInfo.setBank_code("");
                }

            }
        });
    }
    @OnClick(R.id.bind)
    public  void onBindBank(){
        String num=ed_num.getText().toString();
        if(ed_name.getText().length()==0){
            XToast.error("请输入持卡人姓名");
            return;
        }
        if(XEmptyUtils.isSpace(num)){
            XToast.error("请输入银行卡号");
            return;
        }
        if(TextUtils.isEmpty(bankInfo.getBank_code())){
            XToast.error("请输入正确的银行卡号");
            return;
        }

        Map<String,Object> params=new HashMap<>();
        params.put("m_id", CacheUtils.getMember().getM_id());
        params.put("bank_name",bankInfo.getBank_name());
         params.put("cardholder",ed_name.getText().toString());
        params.put("bank_no",ed_num.getText().toString());
        params.put("is_defalut",0);
        params.put("bank_code",bankInfo.getBank_code());
        XHttp.obtain().post(WGJAPI.BIND_BANK_URL, params, new HttpCallBack<Response>() {
            @Override
            public void onSuccess(Response response) {
                XToast.success("绑定成功");
                finish();
            }

            @Override
            public void onFailed(int i, String s, boolean b) {
                XToast.success("绑定失败");
            }
        });

    }
    @OnClick(R.id.tv_support_bank)
    public void supportBank(){
        startActivity(new Intent(this,SupportBankActivity.class));
    }

}
